home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 001a / rzsz1291.zip / SZ.C < prev    next >
C/C++ Source or Header  |  1991-12-07  |  36KB  |  1,623 lines

  1. #define VERSION "3.18 12-07-91"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*% cc -compat -M2 -Ox -K -i -DTXBSIZE=16384  -DNFGVMIN -DREADCHECK sz.c -lx -o sz; size sz
  5.  
  6. <-xtx-*> cc -Osal -DTXBSIZE=32768  -DSV sz.c -lx -o $B/sz; size $B/sz
  7.  
  8.  ****************************************************************************
  9.  *
  10.  * sz.c By Chuck Forsberg,  Omen Technology INC
  11.  *
  12.  ****************************************************************************
  13.  *
  14.  * Typical Unix/Xenix/Clone compiles:
  15.  *
  16.  *    cc -O sz.c -o sz        USG (SYS III/V) Unix
  17.  *    cc -O -DSV sz.c -o sz        Sys V Release 2 with non-blocking input
  18.  *                    Define to allow reverse channel checking
  19.  *    cc -O -DV7  sz.c -o sz        Unix Version 7, 2.8 - 4.3 BSD
  20.  *
  21.  *    cc -O -K -i -DNFGVMIN -DREADCHECK sz.c -lx -o sz    Classic Xenix
  22.  *
  23.  *    ln sz sb            **** All versions ****
  24.  *    ln sz sx            **** All versions ****
  25.  *
  26.  ****************************************************************************
  27.  ****************************************************************************
  28.  *
  29.  *
  30.  * A program for Unix to send files and commands to computers running
  31.  *  Professional-YAM, PowerCom, YAM, IMP, or programs supporting Y/XMODEM.
  32.  *    Copyright 1991 Omen Technology Inc All Rights Reserved
  33.  *
  34.  *  Sz uses buffered I/O to greatly reduce CPU time compared to UMODEM.
  35.  *
  36.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  37.  *
  38.  * 
  39.  *    This version implements numerous enhancements including ZMODEM
  40.  *    Run Length Encoding and variable length headers.  These
  41.  *    features were not funded by the original Telenet development
  42.  *    contract.
  43.  * 
  44.  * This software may be freely used for non commercial and
  45.  * educational (didactic only) purposes.  This software may also
  46.  * be freely used to support file transfer operations to or from
  47.  * licensed Omen Technology products.  Any programs which use
  48.  * part or all of this software must be provided in source form
  49.  * with this notice intact except by written permission from Omen
  50.  * Technology Incorporated.
  51.  * 
  52.  * Use of this software for commercial or administrative purposes
  53.  * except when exclusively limited to interfacing Omen Technology
  54.  * products requires a per port license payment of $20.00 US per
  55.  * port (less in quantity).  Use of this code by inclusion,
  56.  * decompilation, reverse engineering or any other means
  57.  * constitutes agreement to these conditions and acceptance of
  58.  * liability to license the materials and payment of reasonable
  59.  * legal costs necessary to enforce this license agreement.
  60.  *
  61.  *
  62.  *        Omen Technology Inc        FAX: 503-621-3745
  63.  *        Post Office Box 4681
  64.  *        Portland OR 97208
  65.  *
  66.  *    This code is made available in the hope it will be useful,
  67.  *    BUT WITHOUT ANY WARRANTY OF ANY KIND OR LIABILITY FOR ANY
  68.  *    DAMAGES OF ANY KIND.
  69.  */
  70.  
  71.  
  72. char *substr(), *getenv();
  73.  
  74. #define LOGFILE "/tmp/szlog"
  75. #include <stdio.h>
  76. #include <signal.h>
  77. #include <ctype.h>
  78. #include <errno.h>
  79. extern int errno;
  80. #define STATIC
  81.  
  82. #define PATHLEN 256
  83. #define OK 0
  84. #define FALSE 0
  85. #ifdef TRUE
  86. #undef TRUE
  87. #endif
  88. #define TRUE 1
  89. #define ERROR (-1)
  90. /* Ward Christensen / CP/M parameters - Don't change these! */
  91. #define ENQ 005
  92. #define CAN ('X'&037)
  93. #define XOFF ('s'&037)
  94. #define XON ('q'&037)
  95. #define SOH 1
  96. #define STX 2
  97. #define EOT 4
  98. #define ACK 6
  99. #define NAK 025
  100. #define CPMEOF 032
  101. #define WANTCRC 0103    /* send C not NAK to get crc not checksum */
  102. #define WANTG 0107    /* Send G not NAK to get nonstop batch xmsn */
  103. #define TIMEOUT (-2)
  104. #define RCDO (-3)
  105. #define GCOUNT (-4)
  106. #define RETRYMAX 10
  107.  
  108.  
  109. #define HOWMANY 2
  110. STATIC int Zmodem=0;        /* ZMODEM protocol requested by receiver */
  111. unsigned Baudrate=4800;        /* Default, set by first mode() call */
  112. STATIC unsigned Effbaud = 4800;
  113. STATIC unsigned Txwindow;    /* Control the size of the transmitted window */
  114. STATIC unsigned Txwspac;    /* Spacing between zcrcq requests */
  115. STATIC unsigned Txwcnt;    /* Counter used to space ack requests */
  116. STATIC long Lrxpos;        /* Receiver's last reported offset */
  117. STATIC int errors;
  118. char endmsg[80] = {0};    /* Possible message to display on exit */
  119.  
  120. #include "rbsb.c"    /* most of the system dependent stuff here */
  121.  
  122. #include "crctab.c"
  123.  
  124. STATIC int Filesleft;
  125. STATIC long Totalleft;
  126.  
  127. /*
  128.  * Attention string to be executed by receiver to interrupt streaming data
  129.  *  when an error is detected.  A pause (0336) may be needed before the
  130.  *  ^C (03) or after it.
  131.  */
  132. #ifdef READCHECK
  133. STATIC char Myattn[] = { 0 };
  134. #else
  135. #ifdef USG
  136. STATIC char Myattn[] = { 03, 0336, 0 };
  137. #endif
  138. #endif
  139.  
  140. FILE *in;
  141.  
  142. #ifdef BADSEEK
  143. STATIC int Canseek = 0;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  144. #ifndef TXBSIZE
  145. #define TXBSIZE 16384        /* Must be power of two, < MAXINT */
  146. #endif
  147. #else
  148. STATIC int Canseek = 1;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  149. #endif
  150.  
  151. #ifdef TXBSIZE
  152. #define TXBMASK (TXBSIZE-1)
  153. STATIC char Txb[TXBSIZE];        /* Circular buffer for file reads */
  154. STATIC char *txbuf = Txb;        /* Pointer to current file segment */
  155. #else
  156. STATIC char txbuf[1024];
  157. #endif
  158. STATIC long vpos = 0;            /* Number of bytes read from file */
  159.  
  160. STATIC char Lastrx;
  161. STATIC char Crcflg;
  162. STATIC int Modem2=0;        /* XMODEM Protocol - don't send pathnames */
  163. STATIC int Restricted=0;    /* restricted; no /.. or ../ in filenames */
  164. STATIC int Fullname=0;        /* transmit full pathname */
  165. STATIC int Unlinkafter=0;    /* Unlink file after it is sent */
  166. STATIC int Dottoslash=0;    /* Change foo.bar.baz to foo/bar/baz */
  167. STATIC int firstsec;
  168. STATIC int errcnt=0;        /* number of files unreadable */
  169. STATIC int blklen=128;        /* length of transmitted records */
  170. STATIC int Optiong;        /* Let it rip no wait for sector ACK's */
  171. STATIC int Eofseen;        /* EOF seen on input set by zfilbuf */
  172. STATIC int BEofseen;        /* EOF seen on input set by fooseek */
  173. STATIC int Totsecs;        /* total number of sectors this file */
  174. STATIC int Filcnt=0;        /* count of number of files opened */
  175. STATIC unsigned Rxbuflen=16384;    /* Receiver's max buffer length */
  176. STATIC int Tframlen = 0;    /* Override for tx frame length */
  177. STATIC int blkopt=0;        /* Override value for zmodem blklen */
  178. STATIC int Rxflags = 0;
  179. STATIC long bytcnt;
  180. STATIC int Wantfcs32 = TRUE;    /* want to send 32 bit FCS */
  181. STATIC char Lzconv;    /* Local ZMODEM file conversion request */
  182. STATIC char Lzmanag;    /* Local ZMODEM file management request */
  183. STATIC int Lskipnocor;
  184. STATIC char Lztrans;
  185. STATIC int Command;        /* Send a command, then exit. */
  186. STATIC char *Cmdstr;        /* Pointer to the command string */
  187. STATIC int Cmdtries = 11;
  188. STATIC int Cmdack1;        /* Rx ACKs command, then do it */
  189. STATIC int Exitcode;
  190. STATIC int Test;        /* 1= Force receiver to send Attn, etc with qbf. */
  191.             /* 2= Character transparency test */
  192. STATIC char *qbf=
  193.  "The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  194. STATIC long Lastsync;        /* Last offset to which we got a ZRPOS */
  195. STATIC int Beenhereb4;        /* How many times we've been ZRPOS'd same place */
  196.  
  197. STATIC jmp_buf tohere;        /* For the interrupt on RX timeout */
  198. STATIC jmp_buf intrjmp;    /* For the interrupt on RX CAN */
  199.  
  200. #ifdef XARGSFILE
  201. char *
  202. mystrsave(s)
  203. char *s;
  204. {
  205.     register char *p;
  206.     char *malloc();
  207.  
  208.     if (p = malloc(strlen(s)+1) ) {
  209.         strcpy(p, s); return p;
  210.     }
  211.     fprintf(stderr, "No memory for mystrsave!\n");
  212.     exit(1);
  213. }
  214.  
  215. /* Remove (presumably) terminating CR and/or LF from string */
  216. uncrlf(s)
  217. register char *s;
  218. {
  219.     for ( ; *s; ++s)
  220.         switch (*s) {
  221.         case '\r':
  222.         case '\n':
  223.             *s = 0;  return;
  224.         }
  225. }
  226. #endif
  227.  
  228.  
  229. /* called by signal interrupt or terminate to clean things up */
  230. bibi(n)
  231. {
  232.     canit(); fflush(stdout); mode(0);
  233.     fprintf(stderr, "sz: caught signal %d; exiting\n", n);
  234.     if (n == SIGQUIT)
  235.         abort();
  236.     if (n == 99)
  237.         fprintf(stderr, "mode(2) in rbsb.c not implemented!!\n");
  238.     exit(3);
  239. }
  240. /* Called when ZMODEM gets an interrupt (^X) */
  241. onintr()
  242. {
  243.     signal(SIGINT, SIG_IGN);
  244.     longjmp(intrjmp, -1);
  245. }
  246.  
  247. STATIC int Zctlesc;    /* Encode control characters */
  248. STATIC int Nozmodem = 0;    /* If invoked as "sb" */
  249. STATIC char *Progname = "sz";
  250. STATIC int Zrwindow = 1400;    /* RX window size (controls garbage count) */
  251. #include "zm.c"
  252.  
  253. #include "zmr.c"
  254.  
  255. #ifdef XARGSFILE
  256. #define XARGSMAX 256
  257. char *xargv[XARGSMAX+1];
  258. #endif
  259.  
  260. main(argc, argv)
  261. char *argv[];
  262. {
  263.     register char *cp;
  264.     register npats;
  265.     int dm;
  266.     char **patts;
  267.  
  268.     if ((cp = getenv("ZNULLS")) && *cp)
  269.         Znulls = atoi(cp);
  270.     if ((cp=getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  271.         Restricted=TRUE;
  272.     inittty();
  273.     chkinvok(argv[0]);
  274.  
  275.     Rxtimeout = 600;
  276.     npats=0;
  277.     if (argc<2)
  278.         usage();
  279.     while (--argc) {
  280.         cp = *++argv;
  281.         if (*cp++ == '-' && *cp) {
  282.             while ( *cp) {
  283.                 switch(*cp++) {
  284.                 case '\\':
  285.                      *cp = toupper(*cp);  continue;
  286.                 case '+':
  287.                     Lzmanag = ZMAPND; break;
  288. #ifdef CSTOPB
  289.                 case '2':
  290.                     Twostop = TRUE; break;
  291. #endif
  292.                 case 'a':
  293.                     if (Nozmodem || Modem2)
  294.                         usage;
  295.                     Lzconv = ZCNL;  break;
  296.                 case 'b':
  297.                     Lzconv = ZCBIN; break;
  298.                 case 'C':
  299.                     if (--argc < 1) {
  300.                         usage();
  301.                     }
  302.                     Cmdtries = atoi(*++argv);
  303.                     break;
  304.                 case 'c':
  305.                     Lzmanag = ZMCHNG;  break;
  306.                 case 'd':
  307.                     ++Dottoslash;
  308.                     /* **** FALL THROUGH TO **** */
  309.                 case 'f':
  310.                     Fullname=TRUE; break;
  311.                 case 'e':
  312.                     Zctlesc = 1; break;
  313.                 case 'k':
  314.                     blklen=1024; break;
  315.                 case 'L':
  316.                     if (--argc < 1) {
  317.                         usage();
  318.                     }
  319.                     blkopt = atoi(*++argv);
  320.                     if (blkopt<24 || blkopt>1024)
  321.                         usage();
  322.                     break;
  323.                 case 'l':
  324.                     if (--argc < 1) {
  325.                         usage();
  326.                     }
  327.                     Tframlen = atoi(*++argv);
  328.                     if (Tframlen<32 || Tframlen>1024)
  329.                         usage();
  330.                     break;
  331.                 case 'N':
  332.                     Lzmanag = ZMNEWL;  break;
  333.                 case 'n':
  334.                     Lzmanag = ZMNEW;  break;
  335.                 case 'o':
  336.                     Wantfcs32 = FALSE; break;
  337.                 case 'p':
  338.                     Lzmanag = ZMPROT;  break;
  339.                 case 'r':
  340.                     if (Lzconv == ZCRESUM)
  341.                         Lzmanag = (Lzmanag & ZMMASK) | ZMCRC;
  342.                     Lzconv = ZCRESUM; break;
  343.                 case 't':
  344.                     if (--argc < 1) {
  345.                         usage();
  346.                     }
  347.                     Rxtimeout = atoi(*++argv);
  348.                     if (Rxtimeout<10 || Rxtimeout>1000)
  349.                         usage();
  350.                     break;
  351.                 case 'T':
  352.                     if (++Test > 1) {
  353.                         chartest(1); chartest(2);
  354.                         mode(0);  exit(0);
  355.                     }
  356.                     break;
  357.                 case 'u':
  358.                     ++Unlinkafter; break;
  359.                 case 'v':
  360.                     ++Verbose; break;
  361.                 case 'w':
  362.                     if (--argc < 1) {
  363.                         usage();
  364.                     }
  365.                     Txwindow = atoi(*++argv);
  366.                     if (Txwindow < 256)
  367.                         Txwindow = 256;
  368.                     Txwindow = (Txwindow/64) * 64;
  369.                     Txwspac = Txwindow/4;
  370.                     if (blkopt > Txwspac
  371.                      || (!blkopt && Txwspac < 1024))
  372.                         blkopt = Txwspac;
  373.                     break;
  374.                 case 'Y':
  375.                     Lskipnocor = TRUE;
  376.                     /* **** FALLL THROUGH TO **** */
  377.                 case 'y':
  378.                     Lzmanag = ZMCLOB; break;
  379.                 case 'Z':
  380.                 case 'z':
  381.                     Lztrans = ZTRLE;  break;
  382.                 default:
  383.                     usage();
  384.                 }
  385.             }
  386.         }
  387.         else if (Command) {
  388.             if (argc != 1) {
  389.                 usage();
  390.             }
  391.             Cmdstr = *argv;
  392.         }
  393.         else if ( !npats && argc>0) {
  394.             if (argv[0][0]) {
  395.                 npats=argc;
  396.                 patts=argv;
  397.             }
  398.         }
  399.     }
  400.     if (npats < 1 && !Command && !Test) 
  401.         usage();
  402.     if (Verbose) {
  403.         if (freopen(LOGFILE, "a", stderr)==NULL) {
  404.             printf("Can't open log file %s\n",LOGFILE);
  405.             exit(2);
  406.         }
  407.         setbuf(stderr, NULL);
  408.     }
  409.     vfile("%s %s for %s\n", Progname, VERSION, OS);
  410.  
  411. #ifdef XARGSFILE
  412.     vfile("npats=%d *patts=%s", npats, *patts);
  413.     if (npats == 1 && !strcmp(XARGSFILE, *patts)) {
  414.         in = fopen(XARGSFILE, "r");
  415.         if (!in) {
  416.             printf(stderr, "Can't open / control file!\n");
  417.             exit(2);
  418.         }
  419.         for (npats=0,argv=patts=xargv; npats<XARGSMAX; ++npats,++argv) {
  420.             if (fgets(txbuf, 1024, in) <= 0)
  421.                 break;
  422.             uncrlf(txbuf);
  423.             *argv = mystrsave(txbuf);
  424.         }
  425.         fclose(in);
  426.     }
  427. #endif
  428.  
  429.     mode(1);
  430.  
  431.     if (signal(SIGINT, bibi) == SIG_IGN) {
  432.         signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  433.     } else {
  434.         signal(SIGINT, bibi); signal(SIGKILL, bibi);
  435.     }
  436. #ifdef SIGQUIT
  437.     signal(SIGQUIT, SIG_IGN);
  438. #endif
  439. #ifdef SIGTERM
  440.     signal(SIGTERM, bibi);
  441. #endif
  442.  
  443.     if ( !Modem2) {
  444.         if (!Nozmodem) {
  445.             printf("rz\r");  fflush(stdout);
  446.         }
  447.         countem(npats, patts);
  448.         if (!Nozmodem) {
  449.             stohdr(0L);
  450.             if (Command)
  451.                 Txhdr[ZF0] = ZCOMMAND;
  452.             zshhdr(4, ZRQINIT, Txhdr);
  453.         }
  454.     }
  455.     fflush(stdout);
  456.  
  457.     if (Command) {
  458.         if (getzrxinit()) {
  459.             Exitcode=1; canit();
  460.         }
  461.         else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  462.             Exitcode=1; canit();
  463.         }
  464.     } else if (wcsend(npats, patts)==ERROR) {
  465.         Exitcode=1;
  466.         canit();
  467.     }
  468.     if (endmsg[0])
  469.         printf("  %s: %s\r\n", Progname, endmsg);
  470.     printf("%s %s finished.\r\n", Progname, VERSION);
  471.     fflush(stdout);
  472.     mode(0);
  473.     if(errcnt || Exitcode) {
  474.         exit(1);
  475.     }
  476.     exit(0);
  477.     /*NOTREACHED*/
  478. }
  479.  
  480. wcsend(argc, argp)
  481. char *argp[];
  482. {
  483.     register n;
  484.  
  485.     Crcflg=FALSE;
  486.     firstsec=TRUE;
  487.     bytcnt = -1;
  488.     if (Nozmodem) {
  489.         printf("Start your YMODEM receive. ");  fflush(stdout);
  490.     }
  491.     for (n=0; n<argc; ++n) {
  492.         Totsecs = 0;
  493.         if (wcs(argp[n])==ERROR)
  494.             return ERROR;
  495.     }
  496.     Totsecs = 0;
  497.     if (Filcnt==0) {    /* bitch if we couldn't open ANY files */
  498.         if (!Nozmodem && !Modem2) {
  499.             Command = TRUE;
  500.             Cmdstr = "echo \"sz: Can't open any requested files\"";
  501.             if (getnak()) {
  502.                 Exitcode=1; canit();
  503.             }
  504.             if (!Zmodem)
  505.                 canit();
  506.             else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  507.                 Exitcode=1; canit();
  508.             }
  509.             Exitcode = 1; return OK;
  510.         }
  511.         canit();
  512.         sprintf(endmsg, "Can't open any requested files");
  513.         return ERROR;
  514.     }
  515.     if (Zmodem)
  516.         saybibi();
  517.     else if ( !Modem2)
  518.         wctxpn("");
  519.     return OK;
  520. }
  521.  
  522. wcs(oname)
  523. char *oname;
  524. {
  525.     register c;
  526.     register char *p, *q;
  527.     struct stat f;
  528.     char name[PATHLEN];
  529.  
  530.     strcpy(name, oname);
  531.  
  532.     if (Restricted) {
  533.         /* restrict pathnames to current tree or uucppublic */
  534.         if ( substr(name, "../")
  535.          || (name[0]== '/' && strncmp(name, PUBDIR, strlen(PUBDIR))) ) {
  536.             canit();  sprintf(endmsg,"Security Violation");
  537.             return ERROR;
  538.         }
  539.     }
  540.  
  541.     in=fopen(oname, ROPMODE);
  542.  
  543.     if (in==NULL) {
  544.         ++errcnt;
  545.         return OK;    /* pass over it, there may be others */
  546.     }
  547.     BEofseen = Eofseen = 0;  vpos = 0;
  548.  
  549.     /* Check for directory or block special files */
  550.     fstat(fileno(in), &f);
  551.     c = f.st_mode & S_IFMT;
  552.     if (c == S_IFDIR || c == S_IFBLK) {
  553.         fclose(in);
  554.         return OK;
  555.     }
  556.  
  557.     ++Filcnt;
  558.     switch (wctxpn(name)) {
  559.     case ZSKIP:
  560.     case ZFERR:
  561.         return OK;
  562.     case OK:
  563.         break;
  564.     default:
  565.         return ERROR;
  566.     }
  567.     if (!Zmodem && wctx(f.st_size))
  568.         return ERROR;
  569.  
  570.     if (Unlinkafter)
  571.         unlink(oname);
  572.  
  573.     return 0;
  574. }
  575.  
  576. /*
  577.  * generate and transmit pathname block consisting of
  578.  *  pathname (null terminated),
  579.  *  file length, mode time and file mode in octal
  580.  *  as provided by the Unix fstat call.
  581.  *  N.B.: modifies the passed name, may extend it!
  582.  */
  583. wctxpn(name)
  584. char *name;
  585. {
  586.     register char *p, *q;
  587.     char name2[PATHLEN];
  588.     struct stat f;
  589.  
  590.     if (Modem2) {
  591.         if (*name && fstat(fileno(in), &f)!= -1) {
  592.             fprintf(stderr, "Sending %s, %ld XMODEM blocks. ",
  593.               name, (127+f.st_size)>>7);
  594.         }
  595.         fprintf(stderr, "Give your local XMODEM receive command now.\r\n");
  596.         fflush(stderr);
  597.         return OK;
  598.     }
  599.     zperr("Awaiting pathname nak for %s", *name?name:"<END>");
  600.     if ( !Zmodem)
  601.         if (getnak())
  602.             return ERROR;
  603.  
  604.     q = (char *) 0;
  605.     if (Dottoslash) {        /* change . to . */
  606.         for (p=name; *p; ++p) {
  607.             if (*p == '/')
  608.                 q = p;
  609.             else if (*p == '.')
  610.                 *(q=p) = '/';
  611.         }
  612.         if (q && strlen(++q) > 8) {    /* If name>8 chars */
  613.             q += 8;            /*   make it .ext */
  614.             strcpy(name2, q);    /* save excess of name */
  615.             *q = '.';
  616.             strcpy(++q, name2);    /* add it back */
  617.         }
  618.     }
  619.  
  620.     for (p=name, q=txbuf ; *p; )
  621.         if ((*q++ = *p++) == '/' && !Fullname)
  622.             q = txbuf;
  623.     *q++ = 0;
  624.     p=q;
  625.     while (q < (txbuf + 1024))
  626.         *q++ = 0;
  627.     if (*name) {
  628.         if (fstat(fileno(in), &f)!= -1)
  629.             sprintf(p, "%lu %lo %o 0 %d %ld", f.st_size, f.st_mtime,
  630.               f.st_mode, Filesleft, Totalleft);
  631.         Totalleft -= f.st_size;
  632.     }
  633.     if (--Filesleft <= 0)
  634.         Filesleft = Totalleft = 0;
  635.     if (Totalleft < 0)
  636.         Totalleft = 0;
  637.  
  638.     /* force 1k blocks if name won't fit in 128 byte block */
  639.     if (txbuf[125])
  640.         blklen=1024;
  641.     else {        /* A little goodie for IMP/KMD */
  642.         txbuf[127] = (f.st_size + 127) >>7;
  643.         txbuf[126] = (f.st_size + 127) >>15;
  644.     }
  645.     if (Zmodem)
  646.         return zsendfile(txbuf, 1+strlen(p)+(p-txbuf));
  647.     if (wcputsec(txbuf, 0, 128)==ERROR)
  648.         return ERROR;
  649.     return OK;
  650. }
  651.  
  652. getnak()
  653. {
  654.     register firstch;
  655.  
  656.     Lastrx = 0;
  657.     for (;;) {
  658.         switch (firstch = readline(800)) {
  659.         case ZPAD:
  660.             if (getzrxinit())
  661.                 return ERROR;
  662.             return FALSE;
  663.         case TIMEOUT:
  664.             sprintf(endmsg, "Timeout waiting for ZRINIT");
  665.             return TRUE;
  666.         case WANTG:
  667. #ifdef MODE2OK
  668.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  669. #endif
  670.             Optiong = TRUE;
  671.             blklen=1024;
  672.         case WANTCRC:
  673.             Crcflg = TRUE;
  674.         case NAK:
  675.             return FALSE;
  676.         case CAN:
  677.             if ((firstch = readline(20)) == CAN && Lastrx == CAN) {
  678.                 sprintf(endmsg, "Got CAN waiting to send file");
  679.                 return TRUE;
  680.             }
  681.         default:
  682.             break;
  683.         }
  684.         Lastrx = firstch;
  685.     }
  686. }
  687.  
  688.  
  689. wctx(flen)
  690. long flen;
  691. {
  692.     register int thisblklen;
  693.     register int sectnum, attempts, firstch;
  694.     long charssent;
  695.  
  696.     charssent = 0;  firstsec=TRUE;  thisblklen = blklen;
  697.     vfile("wctx:file length=%ld", flen);
  698.  
  699.     while ((firstch=readline(Rxtimeout))!=NAK && firstch != WANTCRC
  700.       && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
  701.         ;
  702.     if (firstch==CAN) {
  703.         zperr("Receiver CANcelled");
  704.         return ERROR;
  705.     }
  706.     if (firstch==WANTCRC)
  707.         Crcflg=TRUE;
  708.     if (firstch==WANTG)
  709.         Crcflg=TRUE;
  710.     sectnum=0;
  711.     for (;;) {
  712.         if (flen <= (charssent + 896L))
  713.             thisblklen = 128;
  714.         if ( !filbuf(txbuf, thisblklen))
  715.             break;
  716.         if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
  717.             return ERROR;
  718.         charssent += thisblklen;
  719.     }
  720.     fclose(in);
  721.     attempts=0;
  722.     do {
  723.         purgeline();
  724.         sendline(EOT);
  725.         flushmo();
  726.         ++attempts;
  727.     }
  728.         while ((firstch=(readline(Rxtimeout)) != ACK) && attempts < RETRYMAX);
  729.     if (attempts == RETRYMAX) {
  730.         zperr("No ACK on EOT");
  731.         return ERROR;
  732.     }
  733.     else
  734.         return OK;
  735. }
  736.  
  737. wcputsec(buf, sectnum, cseclen)
  738. char *buf;
  739. int sectnum;
  740. int cseclen;    /* data length of this sector to send */
  741. {
  742.     register checksum, wcj;
  743.     register char *cp;
  744.     unsigned oldcrc;
  745.     int firstch;
  746.     int attempts;
  747.  
  748.     firstch=0;    /* part of logic to detect CAN CAN */
  749.  
  750.     if (Verbose>2)
  751.         fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );
  752.     else if (Verbose>1)
  753.         fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
  754.     for (attempts=0; attempts <= RETRYMAX; attempts++) {
  755.         Lastrx= firstch;
  756.         sendline(cseclen==1024?STX:SOH);
  757.         sendline(sectnum);
  758.         sendline(-sectnum -1);
  759.         oldcrc=checksum=0;
  760.         for (wcj=cseclen,cp=buf; --wcj>=0; ) {
  761.             sendline(*cp);
  762.             oldcrc=updcrc((0377& *cp), oldcrc);
  763.             checksum += *cp++;
  764.         }
  765.         if (Crcflg) {
  766.             oldcrc=updcrc(0,updcrc(0,oldcrc));
  767.             sendline((int)oldcrc>>8);
  768.             sendline((int)oldcrc);
  769.         }
  770.         else
  771.             sendline(checksum);
  772.         flushmo();
  773.  
  774.         if (Optiong) {
  775.             firstsec = FALSE; return OK;
  776.         }
  777.         firstch = readline(Rxtimeout);
  778. gotnak:
  779.         switch (firstch) {
  780.         case CAN:
  781.             if(Lastrx == CAN) {
  782. cancan:
  783.                 zperr("Cancelled");  return ERROR;
  784.             }
  785.             break;
  786.         case TIMEOUT:
  787.             zperr("Timeout on sector ACK"); continue;
  788.         case WANTCRC:
  789.             if (firstsec)
  790.                 Crcflg = TRUE;
  791.         case NAK:
  792.             zperr("NAK on sector"); continue;
  793.         case ACK: 
  794.             firstsec=FALSE;
  795.             Totsecs += (cseclen>>7);
  796.             return OK;
  797.         case ERROR:
  798.             zperr("Got burst for sector ACK"); break;
  799.         default:
  800.             zperr("Got %02x for sector ACK", firstch); break;
  801.         }
  802.         for (;;) {
  803.             Lastrx = firstch;
  804.             if ((firstch = readline(Rxtimeout)) == TIMEOUT)
  805.                 break;
  806.             if (firstch == NAK || firstch == WANTCRC)
  807.                 goto gotnak;
  808.             if (firstch == CAN && Lastrx == CAN)
  809.                 goto cancan;
  810.         }
  811.     }
  812.     zperr("Retry Count Exceeded");
  813.     return ERROR;
  814. }
  815.  
  816. /* fill buf with count chars padding with ^Z for CPM */
  817. filbuf(buf, count)
  818. register char *buf;
  819. {
  820.     register c, m;
  821.  
  822.     m = read(fileno(in), buf, count);
  823.     if (m <= 0)
  824.         return 0;
  825.     while (m < count)
  826.         buf[m++] = 032;
  827.     return count;
  828. }
  829.  
  830. /* Fill buffer with blklen chars */
  831. zfilbuf()
  832. {
  833.     int n;
  834.  
  835. #ifdef TXBSIZE
  836.     vfile("zfilbuf: bytcnt =%lu vpos=%lu blklen=%d", bytcnt, vpos, blklen);
  837.     /* We assume request is within buffer, or just beyond */
  838.     txbuf = Txb + (bytcnt & TXBMASK);
  839.     if (vpos <= bytcnt) {
  840.         n = fread(txbuf, 1, blklen, in);
  841.  
  842.         vpos += n;
  843.         if (n < blklen)
  844.             Eofseen = 1;
  845.         vfile("zfilbuf: n=%d vpos=%lu Eofseen=%d", n, vpos, Eofseen);
  846.         return n;
  847.     }
  848.     if (vpos >= (bytcnt+blklen))
  849.         return blklen;
  850.     /* May be a short block if crash recovery etc. */
  851.     Eofseen = BEofseen;
  852.     return (vpos - bytcnt);
  853. #else
  854.     n = fread(txbuf, 1, blklen, in);
  855.     if (n < blklen)
  856.         Eofseen = 1;
  857.     return n;
  858. #endif
  859. }
  860.  
  861. #ifdef TXBSIZE
  862. /* Replacement for brain damaged fseek function.  Returns 0==success */
  863. fooseek(fptr, pos, whence)
  864. FILE *fptr;
  865. long pos;
  866. {
  867.     long m, n;
  868.  
  869.     vfile("fooseek: pos =%lu vpos=%lu Canseek=%d", pos, vpos, Canseek);
  870.     /* Seek offset < current buffer */
  871.     if (pos < (vpos -TXBSIZE +1024)) {
  872.         BEofseen = 0;
  873.         if (Canseek > 0) {
  874.             vpos = pos & ~TXBMASK;
  875.             if (vpos >= pos)
  876.                 vpos -= TXBSIZE;
  877.             if (fseek(fptr, vpos, 0))
  878.                 return 1;
  879.         }
  880.         else if (Canseek == 0) {
  881.             if (fseek(fptr, vpos = 0L, 0))
  882.                 return 1;
  883.         } else
  884.             return 1;
  885.         while (vpos < pos) {
  886.             n = fread(Txb, 1, TXBSIZE, fptr);
  887.             vpos += n;
  888.             vfile("n=%d vpos=%ld", n, vpos);
  889.             if (n < TXBSIZE) {
  890.                 BEofseen = 1;
  891.                 break;
  892.             }
  893.         }
  894.         vfile("vpos=%ld", vpos);
  895.         return 0;
  896.     }
  897.     /* Seek offset > current buffer (Crash Recovery, etc.) */
  898.     if (pos > vpos) {
  899.         if (Canseek)
  900.             if (fseek(fptr, vpos = (pos & ~TXBMASK), 0))
  901.                 return 1;
  902.         while (vpos <= pos) {
  903.             txbuf = Txb + (vpos & TXBMASK);
  904.             m = TXBSIZE - (vpos & TXBMASK);
  905.             vfile("m=%ld vpos=%ld", m,vpos);
  906.                 n = fread(txbuf, 1, m, fptr);
  907.             vfile("n=%ld vpos=%ld", n,vpos);
  908.             vpos += n;
  909.             vfile("bo=%d m=%ld vpos=%ld", txbuf-Txb,m,vpos);
  910.             if (n < m) {
  911.                 BEofseen = 1;
  912.                 break;
  913.             }
  914.         }
  915.         return 0;
  916.     }
  917.     /* Seek offset is within current buffer */
  918.     vfile("within buffer: vpos=%ld", vpos);
  919.     return 0;
  920. }
  921. #define fseek fooseek
  922. #endif
  923.  
  924.  
  925. /*
  926.  * Log an error
  927.  */
  928. /*VARARGS1*/
  929. zperr(s,p,u)
  930. char *s, *p, *u;
  931. {
  932.     if (Verbose <= 0)
  933.         return;
  934.     fprintf(stderr, "Retry %d: ", errors);
  935.     fprintf(stderr, s, p, u);
  936.     fprintf(stderr, "\n");
  937. }
  938.  
  939. /*
  940.  * substr(string, token) searches for token in string s
  941.  * returns pointer to token within string if found, NULL otherwise
  942.  */
  943. char *
  944. substr(s, t)
  945. register char *s,*t;
  946. {
  947.     register char *ss,*tt;
  948.     /* search for first char of token */
  949.     for (ss=s; *s; s++)
  950.         if (*s == *t)
  951.             /* compare token with substring */
  952.             for (ss=s,tt=t; ;) {
  953.                 if (*tt == 0)
  954.                     return s;
  955.                 if (*ss++ != *tt++)
  956.                     break;
  957.             }
  958.     return NULL;
  959. }
  960.  
  961. char *usinfo[] = {
  962.     "Send Files and Commands with ZMODEM/YMODEM/XMODEM Protocol\n",
  963.     "Usage:    sz [-2+abcdefklLnNuvwyY] [-] file ...",
  964.     "\t    zcommand [-2Cev] COMMAND",
  965.     "\t    zcommandi [-2Cev] COMMAND",
  966.     "\t    sb [-2adfkuv] [-] file ...",
  967.     "\t    sx [-2akuv] [-] file",
  968.     "\nSee sz.doc for option descriptions and licensing information.",
  969.     ""
  970. };
  971.  
  972. usage()
  973. {
  974.     char **pp;
  975.  
  976.     for (pp=usinfo; **pp; ++pp)
  977.         fprintf(stderr, "%s\n", *pp);
  978.     fprintf(stderr, "\n%s %s for %s by Chuck Forsberg, Omen Technology INC\n",
  979.      Progname, VERSION, OS);
  980.     fprintf(stderr, "\t\t\042The High Reliability Software\042\n");
  981.     fprintf(stderr,"\tCopyright 1991 Omen Technology INC All Rights Reserved\n");
  982.     exit(3);
  983. }
  984.  
  985. /*
  986.  * Get the receiver's init parameters
  987.  */
  988. getzrxinit()
  989. {
  990.     register n;
  991.     struct stat f;
  992.  
  993.     for (n=10; --n>=0; ) {
  994.         
  995.         switch (zgethdr(Rxhdr, 1)) {
  996.         case ZCHALLENGE:    /* Echo receiver's challenge numbr */
  997.             stohdr(Rxpos);
  998.             zshhdr(4, ZACK, Txhdr);
  999.             continue;
  1000.         case ZCOMMAND:        /* They didn't see out ZRQINIT */
  1001.             stohdr(0L);
  1002.             zshhdr(4, ZRQINIT, Txhdr);
  1003.             continue;
  1004.         case ZRINIT:
  1005.             Rxflags = 0377 & Rxhdr[ZF0];
  1006.             Usevhdrs = Rxhdr[ZF1] & CANVHDR;
  1007.             Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
  1008.             Zctlesc |= Rxflags & TESCCTL;
  1009.             Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
  1010.             if ( !(Rxflags & CANFDX))
  1011.                 Txwindow = 0;
  1012.             vfile("Rxbuflen=%d Tframlen=%d", Rxbuflen, Tframlen);
  1013.             signal(SIGINT, SIG_IGN);
  1014. #ifdef MODE2OK
  1015.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  1016. #endif
  1017.  
  1018. #ifndef READCHECK
  1019. #ifndef USG
  1020.             /* Use 1024 byte frames if no sample/interrupt */
  1021.             if (Rxbuflen < 32 || Rxbuflen > 1024) {
  1022.                 Rxbuflen = 1024;
  1023.                 vfile("Rxbuflen=%d", Rxbuflen);
  1024.             }
  1025. #endif
  1026. #endif
  1027.  
  1028.             /* Override to force shorter frame length */
  1029.             if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))
  1030.                 Rxbuflen = Tframlen;
  1031.             if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))
  1032.                 Rxbuflen = Tframlen;
  1033.             vfile("Rxbuflen=%d", Rxbuflen);
  1034.  
  1035.             /* If using a pipe for testing set lower buf len */
  1036.             fstat(0, &f);
  1037.             if ((f.st_mode & S_IFMT) != S_IFCHR) {
  1038.                 Rxbuflen = 1024;
  1039.             }
  1040.  
  1041.  
  1042.             /*
  1043.              * If input is not a regular file, force ACK's to
  1044.              *  prevent running beyond the buffer limits
  1045.              */
  1046.             if ( !Command) {
  1047.                 fstat(fileno(in), &f);
  1048.                 if ((f.st_mode & S_IFMT) != S_IFREG) {
  1049.                     Canseek = -1;
  1050. #ifdef TXBSIZE
  1051.                     Txwindow = TXBSIZE - 1024;
  1052.                     Txwspac = TXBSIZE/4;
  1053. #else
  1054.                     return ERROR;
  1055. #endif
  1056.                 }
  1057.             }
  1058.  
  1059.             /* Set initial subpacket length */
  1060.             if (blklen < 1024) {    /* Command line override? */
  1061.                 if (Effbaud > 300)
  1062.                     blklen = 256;
  1063.                 if (Effbaud > 1200)
  1064.                     blklen = 512;
  1065.                 if (Effbaud > 2400)
  1066.                     blklen = 1024;
  1067.             }
  1068.             if (Rxbuflen && blklen>Rxbuflen)
  1069.                 blklen = Rxbuflen;
  1070.             if (blkopt && blklen > blkopt)
  1071.                 blklen = blkopt;
  1072.             vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);
  1073.             vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);
  1074.  
  1075.  
  1076.             if (Lztrans == ZTRLE && (Rxflags & CANRLE))
  1077.                 Txfcs32 = 2;
  1078.             else
  1079.                 Lztrans = 0;
  1080.  
  1081.             return (sendzsinit());
  1082.         case ZCAN:
  1083.         case TIMEOUT:
  1084.             return ERROR;
  1085.         case ZRQINIT:
  1086.             if (Rxhdr[ZF0] == ZCOMMAND)
  1087.                 continue;
  1088.         default:
  1089.             zshhdr(4, ZNAK, Txhdr);
  1090.             continue;
  1091.         }
  1092.     }
  1093.     return ERROR;
  1094. }
  1095.  
  1096. /* Send send-init information */
  1097. sendzsinit()
  1098. {
  1099.     register c;
  1100.  
  1101.     if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))
  1102.         return OK;
  1103.     errors = 0;
  1104.     for (;;) {
  1105.         stohdr(0L);
  1106. #ifdef ALTCANOFF
  1107.         Txhdr[ALTCOFF] = ALTCANOFF;
  1108. #endif
  1109.         if (Zctlesc) {
  1110.             Txhdr[ZF0] |= TESCCTL; zshhdr(4, ZSINIT, Txhdr);
  1111.         }
  1112.         else
  1113.             zsbhdr(4, ZSINIT, Txhdr);
  1114.         zsdata(Myattn, ZATTNLEN, ZCRCW);
  1115.         c = zgethdr(Rxhdr, 1);
  1116.         switch (c) {
  1117.         case ZCAN:
  1118.             return ERROR;
  1119.         case ZACK:
  1120.             return OK;
  1121.         default:
  1122.             if (++errors > 19)
  1123.                 return ERROR;
  1124.             continue;
  1125.         }
  1126.     }
  1127. }
  1128.  
  1129. /* Send file name and related info */
  1130. zsendfile(buf, blen)
  1131. char *buf;
  1132. {
  1133.     register c;
  1134.     register UNSL long crc;
  1135.     long lastcrcrq = -1;
  1136.     char *p;
  1137.  
  1138.     for (errors=0; ++errors<11;) {
  1139.         Txhdr[ZF0] = Lzconv;    /* file conversion request */
  1140.         Txhdr[ZF1] = Lzmanag;    /* file management request */
  1141.         if (Lskipnocor)
  1142.             Txhdr[ZF1] |= ZMSKNOLOC;
  1143.         Txhdr[ZF2] = Lztrans;    /* file transport request */
  1144.         Txhdr[ZF3] = 0;
  1145.         zsbhdr(4, ZFILE, Txhdr);
  1146.         zsdata(buf, blen, ZCRCW);
  1147. again:
  1148.         c = zgethdr(Rxhdr, 1);
  1149.         switch (c) {
  1150.         case ZRINIT:
  1151.             while ((c = readline(50)) > 0)
  1152.                 if (c == ZPAD) {
  1153.                     goto again;
  1154.                 }
  1155.             continue;
  1156.         case ZCAN:
  1157.         case TIMEOUT:
  1158.         case ZABORT:
  1159.         case ZFIN:
  1160.             sprintf(endmsg, "Got %s on pathname", frametypes[c+FTOFFSET]);
  1161.             return ERROR;
  1162.         default:
  1163.             sprintf(endmsg, "Got %d frame type on pathname", c);
  1164.             continue;
  1165.         case ERROR:
  1166.         case ZNAK:
  1167.             continue;
  1168.         case ZCRC:
  1169.             if (Rxpos != lastcrcrq) {
  1170.                 lastcrcrq = Rxpos;
  1171.                 crc = 0xFFFFFFFFL;
  1172.                 if (Canseek >= 0) {
  1173.                     fseek(in, 0L, 0);
  1174.                     while (((c = getc(in)) != EOF) && --lastcrcrq)
  1175.                         crc = UPDC32(c, crc);
  1176.                     crc = ~crc;
  1177.                     clearerr(in);    /* Clear possible EOF */
  1178.                     lastcrcrq = Rxpos;
  1179.                 }
  1180.             }
  1181.             stohdr(crc);
  1182.             zsbhdr(4, ZCRC, Txhdr);
  1183.             goto again;
  1184.         case ZFERR:
  1185.         case ZSKIP:
  1186.             sprintf(endmsg, "File skipped by receiver request");
  1187.             fclose(in); return c;
  1188.         case ZRPOS:
  1189.             /*
  1190.              * Suppress zcrcw request otherwise triggered by
  1191.              * lastyunc==bytcnt
  1192.              */
  1193.             if (fseek(in, Rxpos, 0))
  1194.                 return ERROR;
  1195.             Lastsync = (bytcnt = Txpos = Lrxpos = Rxpos) -1;
  1196.             return zsendfdata();
  1197.         }
  1198.     }
  1199.     fclose(in); return ERROR;
  1200. }
  1201.  
  1202. /* Send the data in the file */
  1203. zsendfdata()
  1204. {
  1205.     register c, e, n;
  1206.     register newcnt;
  1207.     register long tcount = 0;
  1208.     int junkcount;        /* Counts garbage chars received by TX */
  1209.     static int tleft = 6;    /* Counter for test mode */
  1210.  
  1211.     junkcount = 0;
  1212.     Beenhereb4 = FALSE;
  1213. somemore:
  1214.     if (setjmp(intrjmp)) {
  1215. waitack:
  1216.         junkcount = 0;
  1217.         c = getinsync(0);
  1218. gotack:
  1219.         switch (c) {
  1220.         default:
  1221.         case ZCAN:
  1222.             fclose(in);
  1223.             return ERROR;
  1224.         case ZSKIP:
  1225.             fclose(in);
  1226.             return c;
  1227.         case ZACK:
  1228.         case ZRPOS:
  1229.             break;
  1230.         case ZRINIT:
  1231.             fclose(in);
  1232.             return OK;
  1233.         }
  1234. #ifdef READCHECK
  1235.         /*
  1236.          * If the reverse channel can be tested for data,
  1237.          *  this logic may be used to detect error packets
  1238.          *  sent by the receiver, in place of setjmp/longjmp
  1239.          *  rdchk(Tty) returns non 0 if a character is available
  1240.          */
  1241.         while (rdchk(Tty)) {
  1242. #ifdef EATSIT
  1243.             switch (checked)
  1244. #else
  1245.             switch (readline(1))
  1246. #endif
  1247.             {
  1248.             case CAN:
  1249.             case ZPAD:
  1250.                 c = getinsync(1);
  1251.                 goto gotack;
  1252.             case XOFF:        /* Wait a while for an XON */
  1253.             case XOFF|0200:
  1254.                 readline(100);
  1255.             }
  1256.         }
  1257. #endif
  1258.     }
  1259.  
  1260.     signal(SIGINT, onintr);
  1261.     newcnt = Rxbuflen;
  1262.     Txwcnt = 0;
  1263.     stohdr(Txpos);
  1264.     zsbhdr(4, ZDATA, Txhdr);
  1265.  
  1266.     /*
  1267.      * Special testing mode.  This should force receiver to Attn,ZRPOS
  1268.      *  many times.  Each time the signal should be caught, causing the
  1269.      *  file to be started over from the beginning.
  1270.      */
  1271.     if (Test) {
  1272.         if ( --tleft)
  1273.             while (tcount < 20000) {
  1274.                 printf(qbf); fflush(stdout);
  1275.                 tcount += strlen(qbf);
  1276. #ifdef READCHECK
  1277.                 while (rdchk(Tty)) {
  1278. #ifdef EATSIT
  1279.                     switch (checked)
  1280. #else
  1281.                     switch (readline(1))
  1282. #endif
  1283.                     {
  1284.                     case CAN:
  1285.                     case ZPAD:
  1286. #ifdef TCFLSH
  1287.                         ioctl(0, TCFLSH, 1);
  1288. #endif
  1289.                         goto waitack;
  1290.                     case XOFF:    /* Wait for XON */
  1291.                     case XOFF|0200:
  1292.                         readline(100);
  1293.                     }
  1294.                 }
  1295. #endif
  1296.             }
  1297.         signal(SIGINT, SIG_IGN); canit();
  1298.         sleep(3); purgeline(); mode(0);
  1299.         printf("\nsz: Tcount = %ld\n", tcount);
  1300.         if (tleft) {
  1301.             printf("ERROR: Interrupts Not Caught\n");
  1302.             exit(1);
  1303.         }
  1304.         exit(0);
  1305.     }
  1306.  
  1307.     do {
  1308.         n = zfilbuf();
  1309.         if (Eofseen)
  1310.             e = ZCRCE;
  1311.         else if (junkcount > 3)
  1312.             e = ZCRCW;
  1313.         else if (bytcnt == Lastsync)
  1314.             e = ZCRCW;
  1315.         else if (Rxbuflen && (newcnt -= n) <= 0)
  1316.             e = ZCRCW;
  1317.         else if (Txwindow && (Txwcnt += n) >= Txwspac) {
  1318.             Txwcnt = 0;  e = ZCRCQ;
  1319.         } else
  1320.             e = ZCRCG;
  1321.         if (Verbose>1)
  1322.             fprintf(stderr, "\r%7ld ZMODEM%s    ",
  1323.               Txpos, Crc32t?" CRC-32":"");
  1324.         zsdata(txbuf, n, e);
  1325.         bytcnt = Txpos += n;
  1326.         if (e == ZCRCW)
  1327.             goto waitack;
  1328. #ifdef READCHECK
  1329.         /*
  1330.          * If the reverse channel can be tested for data,
  1331.          *  this logic may be used to detect error packets
  1332.          *  sent by the receiver, in place of setjmp/longjmp
  1333.          *  rdchk(Tty) returns non 0 if a character is available
  1334.          */
  1335.         fflush(stdout);
  1336.         while (rdchk(Tty)) {
  1337. #ifdef EATSIT
  1338.             switch (checked)
  1339. #else
  1340.             switch (readline(1))
  1341. #endif
  1342.             {
  1343.             case CAN:
  1344.             case ZPAD:
  1345.                 c = getinsync(1);
  1346.                 if (c == ZACK)
  1347.                     break;
  1348. #ifdef TCFLSH
  1349.                 ioctl(0, TCFLSH, 1);
  1350. #endif
  1351.                 /* zcrce - dinna wanna starta ping-pong game */
  1352.                 zsdata(txbuf, 0, ZCRCE);
  1353.                 goto gotack;
  1354.             case XOFF:        /* Wait a while for an XON */
  1355.             case XOFF|0200:
  1356.                 readline(100);
  1357.             default:
  1358.                 ++junkcount;
  1359.             }
  1360.         }
  1361. #endif    /* READCHECK */
  1362.         if (Txwindow) {
  1363.             while ((tcount = (Txpos - Lrxpos)) >= Txwindow) {
  1364.                 vfile("%ld window >= %u", tcount, Txwindow);
  1365.                 if (e != ZCRCQ)
  1366.                     zsdata(txbuf, 0, e = ZCRCQ);
  1367.                 c = getinsync(1);
  1368.                 if (c != ZACK) {
  1369. #ifdef TCFLSH
  1370.                     ioctl(0, TCFLSH, 1);
  1371. #endif
  1372.                     zsdata(txbuf, 0, ZCRCE);
  1373.                     goto gotack;
  1374.                 }
  1375.             }
  1376.             vfile("window = %ld", tcount);
  1377.         }
  1378.     } while (!Eofseen);
  1379.     signal(SIGINT, SIG_IGN);
  1380.  
  1381.     for (;;) {
  1382.         stohdr(Txpos);
  1383.         zsbhdr(4, ZEOF, Txhdr);
  1384.         switch (getinsync(0)) {
  1385.         case ZACK:
  1386.             continue;
  1387.         case ZRPOS:
  1388.             goto somemore;
  1389.         case ZRINIT:
  1390.             fclose(in);
  1391.             return OK;
  1392.         case ZSKIP:
  1393.             fclose(in);
  1394.             sprintf(endmsg, "File skipped by receiver request");
  1395.             return c;
  1396.         default:
  1397.             sprintf(endmsg, "Got %d trying to send end of file", c);
  1398.             fclose(in);
  1399.             return ERROR;
  1400.         }
  1401.     }
  1402. }
  1403.  
  1404. /*
  1405.  * Respond to receiver's complaint, get back in sync with receiver
  1406.  */
  1407. getinsync(flag)
  1408. {
  1409.     register c;
  1410.  
  1411.     for (;;) {
  1412.         if (Test) {
  1413.             printf("\r\n\n\n***** Signal Caught *****\r\n");
  1414.             Rxpos = 0; c = ZRPOS;
  1415.         } else
  1416.             c = zgethdr(Rxhdr, 0);
  1417.         switch (c) {
  1418.         case ZCAN:
  1419.         case ZABORT:
  1420.         case ZFIN:
  1421.         case TIMEOUT:
  1422.             sprintf(endmsg, "Got %s sending data", frametypes[c+FTOFFSET]);
  1423.             return ERROR;
  1424.         case ZRPOS:
  1425.             /* ************************************* */
  1426.             /*  If sending to a buffered modem, you  */
  1427.             /*   might send a break at this point to */
  1428.             /*   dump the modem's buffer.         */
  1429.             clearerr(in);    /* In case file EOF seen */
  1430.             if (fseek(in, Rxpos, 0))
  1431.                 return ERROR;
  1432.             Eofseen = 0;
  1433.             bytcnt = Lrxpos = Txpos = Rxpos;
  1434.             if (Lastsync == Rxpos) {
  1435.                 if (++Beenhereb4 > 12) {
  1436.                     sprintf(endmsg, "Can't send block");
  1437.                     return ERROR;
  1438.                 }
  1439.                 if (Beenhereb4 > 4)
  1440.                     if (blklen > 32)
  1441.                         blklen /= 2;
  1442.             }
  1443.             Lastsync = Rxpos;
  1444.             return c;
  1445.         case ZACK:
  1446.             Lrxpos = Rxpos;
  1447.             if (flag || Txpos == Rxpos)
  1448.                 return ZACK;
  1449.             continue;
  1450.         case ZRINIT:
  1451.             return c;
  1452.         case ZSKIP:
  1453.             sprintf(endmsg, "File skipped by receiver request");
  1454.             return c;
  1455.         case ERROR:
  1456.         default:
  1457.             zsbhdr(4, ZNAK, Txhdr);
  1458.             continue;
  1459.         }
  1460.     }
  1461. }
  1462.  
  1463.  
  1464. /* Say "bibi" to the receiver, try to do it cleanly */
  1465. saybibi()
  1466. {
  1467.     for (;;) {
  1468.         stohdr(0L);        /* CAF Was zsbhdr - minor change */
  1469.         zshhdr(4, ZFIN, Txhdr);    /*  to make debugging easier */
  1470.         switch (zgethdr(Rxhdr, 0)) {
  1471.         case ZFIN:
  1472.             sendline('O'); sendline('O'); flushmo();
  1473.         case ZCAN:
  1474.         case TIMEOUT:
  1475.             return;
  1476.         }
  1477.     }
  1478. }
  1479.  
  1480. /* Send command and related info */
  1481. zsendcmd(buf, blen)
  1482. char *buf;
  1483. {
  1484.     register c;
  1485.     long cmdnum;
  1486.  
  1487.     cmdnum = getpid();
  1488.     errors = 0;
  1489.     for (;;) {
  1490.         stohdr(cmdnum);
  1491.         Txhdr[ZF0] = Cmdack1;
  1492.         zsbhdr(4, ZCOMMAND, Txhdr);
  1493.         zsdata(buf, blen, ZCRCW);
  1494. listen:
  1495.         Rxtimeout = 100;        /* Ten second wait for resp. */
  1496.         Usevhdrs = 0;        /* Allow rx to send fixed len headers */
  1497.         c = zgethdr(Rxhdr, 1);
  1498.  
  1499.         switch (c) {
  1500.         case ZRINIT:
  1501.             goto listen;    /* CAF 8-21-87 */
  1502.         case ERROR:
  1503.         case GCOUNT:
  1504.         case TIMEOUT:
  1505.             if (++errors > Cmdtries)
  1506.                 return ERROR;
  1507.             continue;
  1508.         case ZCAN:
  1509.         case ZABORT:
  1510.         case ZFIN:
  1511.         case ZSKIP:
  1512.         case ZRPOS:
  1513.             return ERROR;
  1514.         default:
  1515.             if (++errors > 20)
  1516.                 return ERROR;
  1517.             continue;
  1518.         case ZCOMPL:
  1519.             Exitcode = Rxpos;
  1520.             saybibi();
  1521.             return OK;
  1522.         case ZRQINIT:
  1523.             vfile("******** RZ *******");
  1524.             system("rz");
  1525.             vfile("******** SZ *******");
  1526.             goto listen;
  1527.         }
  1528.     }
  1529. }
  1530.  
  1531. /*
  1532.  * If called as sb use YMODEM protocol
  1533.  */
  1534. chkinvok(s)
  1535. char *s;
  1536. {
  1537.     register char *p;
  1538.  
  1539.     p = s;
  1540.     while (*p == '-')
  1541.         s = ++p;
  1542.     while (*p)
  1543.         if (*p++ == '/')
  1544.             s = p;
  1545.     if (*s == 'v') {
  1546.         Verbose=1; ++s;
  1547.     }
  1548.     Progname = s;
  1549.     if (s[0]=='z' && s[1] == 'c') {
  1550.         Command = TRUE;
  1551.         if (s[8] == 'i')
  1552.             Cmdack1 = ZCACK1;
  1553.     }
  1554.     if (s[0]=='s' && s[1]=='b') {
  1555.         Nozmodem = TRUE; blklen=1024;
  1556.     }
  1557.     if (s[0]=='s' && s[1]=='x') {
  1558.         Modem2 = TRUE;
  1559.     }
  1560. }
  1561.  
  1562. countem(argc, argv)
  1563. register char **argv;
  1564. {
  1565.     register c;
  1566.     struct stat f;
  1567.  
  1568.     for (Totalleft = 0, Filesleft = 0; --argc >=0; ++argv) {
  1569.         f.st_size = -1;
  1570.         if (Verbose>2) {
  1571.             fprintf(stderr, "\nCountem: %03d %s ", argc, *argv);
  1572.             fflush(stderr);
  1573.         }
  1574.         if (access(*argv, 04) >= 0 && stat(*argv, &f) >= 0) {
  1575.             c = f.st_mode & S_IFMT;
  1576.             if (c != S_IFDIR && c != S_IFBLK) {
  1577.                 ++Filesleft;  Totalleft += f.st_size;
  1578.             }
  1579.         }
  1580.         if (Verbose>2)
  1581.             fprintf(stderr, " %ld", f.st_size);
  1582.     }
  1583.     if (Verbose>2)
  1584.         fprintf(stderr, "\ncountem: Total %d %ld\n",
  1585.           Filesleft, Totalleft);
  1586. }
  1587.  
  1588. chartest(m)
  1589. {
  1590.     register n;
  1591.  
  1592.     mode(m);
  1593.     printf("\r\n\nCharacter Transparency Test Mode %d\r\n", m);
  1594.     printf("If Pro-YAM/ZCOMM is not displaying ^M hit ALT-V NOW.\r\n");
  1595.     printf("Hit Enter.\021");  fflush(stdout);
  1596.     readline(500);
  1597.  
  1598.     for (n = 0; n < 256; ++n) {
  1599.         if (!(n%8))
  1600.             printf("\r\n");
  1601.         printf("%02x ", n);  fflush(stdout);
  1602.         sendline(n);    flushmo();
  1603.         printf("  ");  fflush(stdout);
  1604.         if (n == 127) {
  1605.             printf("Hit Enter.\021");  fflush(stdout);
  1606.             readline(500);
  1607.             printf("\r\n");  fflush(stdout);
  1608.         }
  1609.     }
  1610.     printf("\021\r\nEnter Characters, echo is in hex.\r\n");
  1611.     printf("Hit SPACE or pause 40 seconds for exit.\r\n");
  1612.  
  1613.     while (n != TIMEOUT && n != ' ') {
  1614.         n = readline(400);
  1615.         printf("%02x\r\n", n);
  1616.         fflush(stdout);
  1617.     }
  1618.     printf("\r\nMode %d character transparency test ends.\r\n", m);
  1619.     fflush(stdout);
  1620. }
  1621.  
  1622. /* End of sz.c */
  1623.